using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
    [SerializeField] Transform State;
    InputSystem inputSystem;
    public PlayerInputActions InputActions;
    Rigidbody body;
    Animator animator;
    bool isAlive = true;
    int MoveDirection = 0;
    public bool isMoving { get; private set; } = false;

    private void Awake()
    {
        foreach (Transform child in State)
            child.gameObject.SetActive(false);
    }

    void Start()
    {
        //输入系统
        inputSystem = GetComponent<InputSystem>();
        InputActions = inputSystem.inputActions;
        inputSystem.Enable();

        InputActions.Player.Move.started += PlayerMove;
        InputActions.Player.Move.canceled += (context) => { MoveDirection = 0; isMoving = false; };

        isAlive = true;
        body = GetComponent<Rigidbody>();
        animator = GetComponentInChildren<Animator>();
    }

    bool isRightward = true;
    void PlayerMove(InputAction.CallbackContext context)
    {
        if (!isAlive) return;

        MoveDirection = (int)context.ReadValue<Vector2>().x;
        if (MoveDirection != 0)
            isMoving = true;
        if (MoveDirection == 1 && isRightward == false)
        {
            transform.localScale = Vector3.one;
            body.MoveRotation(Quaternion.identity);
            isRightward = true;
        }
        else if (MoveDirection == -1 && isRightward == true)
        {
            transform.localScale = new Vector3(1, 1, -1);
            body.MoveRotation(Quaternion.Euler(0, 180, 0));
            isRightward = false;
        }
    }

    //bool isWrite = true;
    //int i = 0;
    //private void Update()
    //{
    //    if (Vector3.Equals(transform.right, Vector3.right) && !isRightward && isWrite == false)
    //    {
    //        isWrite = true;
    //        Debug.Log("error:" + ++i);
    //    }
    //    else if (Vector3.Equals(transform.right, Vector3.right) && isRightward && isWrite == true)
    //    {
    //        isWrite = false;
    //    }
    //}
    public GameObject FindState(string stateName) => State.Find(stateName).gameObject;

    [HideInInspector] public UnityEvent OnPlayerDie = new();
    public void PlayerDie()
    {
        isAlive = false;
        animator.SetTrigger("Die");
        OnPlayerDie.Invoke();
    }

    [HideInInspector] public UnityEvent OnPlayerWin = new();
    public void PlayerWin()
    {
        isAlive = false;
        animator.SetTrigger("Win");
        OnPlayerWin.Invoke();
    }

    private void OnCollisionEnter(Collision collision)
    {
        //if (collision.gameObject.CompareTag("aa"))
        //{
        //    OnJumpCountAdd.Invoke();
        //}
    }

    public bool isOnGroud =>
         Physics.Raycast(transform.position + transform.up, -transform.up, feetDistance, groundLayer);

    [Header("--Feet--")]
    [SerializeField] float feetDistance;
    [SerializeField] LayerMask groundLayer;

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawRay(transform.position + transform.up, -transform.up * feetDistance);
    }

    [Header("--InSky--")]
    [SerializeField] float speedInSky = 2;
    public float SpeedInSky => speedInSky;
    [SerializeField] int maxJumpCount = 2;
    [SerializeField] int currentJumpCount = 2;
    public bool canJump => currentJumpCount > 0;
    public void Jump() => currentJumpCount--;
    public void JumpCountAdd()
    {
        if (currentJumpCount < maxJumpCount)
            currentJumpCount++;
    }
    public void isGrounded() => currentJumpCount = maxJumpCount;
}
